<?php
namespace App\Controller\App;

use App\Query\UserPayQuery;
use App\Query\ConfigQuery;
use App\Repository\ShopRepository;

class ShopController extends Controller
{
    // 申请商城
    public function index()
    {
        $user = $this->auth();
        
        // 判断我是否可以申请
        if($user->shopMy)
        {
            $this->error('shop');
        }
        
        // 判断开店条件
        if($user->moneyAllHas(ConfigQuery::get()['shop_money']))
        {
            if(ShopRepository::make()->create($user, $this->inputs('name')))
            {
                $this->message('ok');
            }
        }
        
        $this->error('error');
    }
    
    // 当前业绩
    public function money()
    {
        $user = $this->auth();
        
        if(!$user->shopMy)
        {
            $this->error('shop_error');
        }
        
        $model = ShopRepository::make($user->shopMy);
        
        // 查询
        $money = UserPayQuery::totalMoney($model->getCheckTime(), $model->model()->userPay());
    
        return compact('money');
    }
}
